package com.plug.controller;

import com.plug.common.consts.Code;
import com.plug.common.model.ExceptionResult;
import com.plug.common.model.JsonResult;
import com.plug.service.IPlugTaskStateService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by admin on 2017/1/16.
 */
@Controller
@RequestMapping("/taskstate")
public class TaskStateController {

    @Autowired
    private IPlugTaskStateService plugTaskStateService;

    @RequestMapping(value = "/loadpage")
    @ResponseBody
    public ResponseEntity<Object> loadpage(@Param("page")Integer page,@Param("size")Integer size){
        try {
            JsonResult jsonResult = new JsonResult();//存放返回前端的数据
            //存放查到的记录
            List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
            list = plugTaskStateService.findByPage(page,size);

            int total=plugTaskStateService.findTotal();//记录总数
            if(list==null && total==0){
                jsonResult.setMessage("暂无任务记录");
            }
            jsonResult.putData("data",list);
            jsonResult.putData("total",total);
            jsonResult.putData("page",page);
            jsonResult.putData("size",size);
            jsonResult.setCode(Code.OK);
            return new ResponseEntity<Object>(jsonResult, HttpStatus.OK);
        }catch (Exception e){
            return new ResponseEntity<Object>(new ExceptionResult(e), HttpStatus.OK);
        }
    }
    @RequestMapping(value = "delete")
    @ResponseBody
    public ResponseEntity<Object> delete(String arr){
        try{
            List<Integer> list = new ArrayList<>();
            String[] jsonArray = arr.split(",");
            for (int i = 0; i < jsonArray.length; i++) {
                list.add(Integer.valueOf(jsonArray[i]));
            }

            int result = plugTaskStateService.deleteBatch(list);

            JsonResult jsonResult = new JsonResult();
                jsonResult.setCode(Code.SUCCESS);
                jsonResult.setMessage("删除成功！");
            return new ResponseEntity<Object>(jsonResult, HttpStatus.OK);
        } catch (Exception e){
            return new ResponseEntity<Object>(new ExceptionResult(e), HttpStatus.NOT_IMPLEMENTED);
        }



    }
}
